package com.drug.service.impl;

import com.drug.domain.DrugAtegory;
import com.drug.domain.Result;
import com.drug.mapper.DrugAtegoryMapper;
import com.drug.service.DrugAtegoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class DrugAtegoryServiceImpl implements DrugAtegoryService {

    @Autowired
    private DrugAtegoryMapper drugAtegoryMapper;

    @Override
    public int addDrugAtegory(Map map) {
        String o = (String) map.get("daNumber");
        int number = Integer.parseInt(o);
        DrugAtegory drugAtegory = new DrugAtegory();
        drugAtegory.setDaNumber(number);
        int i = drugAtegoryMapper.insertDrugAtegory(drugAtegory);

        Integer daId=0;
        if (i==1){
            daId= drugAtegory.getDaId();
        }
        return daId ;

    }


    /**
     * 添加商品名
     * @param
     * @return
     */
    @Override
    public int insertDrugAtegoryName(DrugAtegory drugAtegory) {

        int i = drugAtegoryMapper.insertDrugAtegoryName(drugAtegory);
        Integer daId=0;
        if (i==1){
            daId= drugAtegory.getDaId();
        }
        return daId;
    }

    @Override
    public int updateDrugAtegorydaName(DrugAtegory drugAtegory) {
        int i = drugAtegoryMapper.updateDrugAtegorydaName(drugAtegory);


        return i;
    }

    /**
     * 根据id回显
     * @param daId
     * @return
     */
    @Override
    public Result selectDaById(Integer daId) {
        Result result = new Result();
        DrugAtegory drugAtegory = drugAtegoryMapper.selectDaById(daId);
        result.setData(drugAtegory);
        return result;
    }

    /**
     * 根据名字查询id
     * @param drugAtegory
     * @return
     */
    @Override
    public int selectidbyname(DrugAtegory drugAtegory) {

        return drugAtegoryMapper.selectidbyname(drugAtegory);
    }

    /**
     * 查询商品名是否已存在
     * @param daName
     * @return
     */
    @Override
    public Result daName(String daName) {
        Result result = new Result();
        String[] daName1 = drugAtegoryMapper.daName();
        for(int i = 0; i<daName1.length;i++){
            if(daName.equals(daName1[i])){
                result.setMsg("商品名已存在");
                result.setCode(0);
                break;
            }else{
                result.setMsg("商品名可用");
                result.setCode(1);
            }
        }
        return result;
    }
}

